package com.lvyq.myplayer.adapter.ranking

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.lvyq.myplayer.model.ranking.RankingInfo
import com.lvyq.myplayer.widget.ranking.TheChartView
import java.util.ArrayList

/**
 * 文件描述：TheChartAdapter
 * 作者：lvyq
 * 创建时间：2025-05-29
 * 修改时间：2025-05-29
 */
class TheChartAdapter (var context:Context?): RecyclerView.Adapter<TheChartAdapter.TheChartHolder>() {

    //数据定义
    var listData= ArrayList<RankingInfo>()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TheChartAdapter.TheChartHolder {
        return  TheChartHolder(TheChartView(parent.context))
    }

    override fun getItemCount(): Int {
       return listData.size;
    }

    override fun onBindViewHolder(holder: TheChartHolder, position: Int) {
        var rankingInfo = listData[position]
        val item = holder.itemView as TheChartView
        item.setData(rankingInfo)
        item.setOnClickListener{
            onItemClickListener?.onItemClick(rankingInfo)
        }
    }

    inner class TheChartHolder(view: View) : RecyclerView.ViewHolder(view) {

    }

    // 定义点击事件接口
    interface OnItemClickListener {
        fun onItemClick(data: RankingInfo)
    }
    private var onItemClickListener: OnItemClickListener? = null
    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.onItemClickListener = listener
    }

    fun initData(list: ArrayList<RankingInfo>) {
        this.listData.clear()
        this.listData.addAll(list)
        notifyDataSetChanged()
    }

}